package com.selfdriving.activity;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.selfdriving.BaseApplication;
import com.selfdriving.BaseObjectListAdapter;
import com.selfdriving.R;
import com.selfdriving.bean.Entity;
import com.selfdriving.bean.FindScenicStrategyScenic;
import com.selfdriving.bean.Hotel;
import com.selfdriving.utils.Constant;
import com.selfdriving.view.MoMoRefreshListView;
import com.selfdriving.view.MoMoRefreshListView.OnCancelListener;
import com.selfdriving.view.MoMoRefreshListView.OnRefreshListener;

public class HotelListActivity extends BaseActivity
{
	@ViewInject(id = R.id.head_break, click = "topBack")
	ImageView topBack;

	@ViewInject(id = R.id.top_text_middle)
	TextView topText;

	@ViewInject(id = R.id.feature_list)
	MoMoRefreshListView feature_list;

	List<Entity> features = new ArrayList<Entity>();

	FeatureAdapter adapter;
	String sid;
	String name;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.feature_list);
		topBack.setVisibility(View.VISIBLE);
		Intent intent = super.getIntent();
		sid = intent.getStringExtra("sid");
		name = intent.getStringExtra("name");
		topText.setText(name + "宾馆");
		post(sid);
		adapter = new FeatureAdapter(mApplication, this, features);
		feature_list.setAdapter(adapter);
		feature_list.setOnRefreshListener(new OnRefreshListener()
		{
			@Override
			public void onRefresh()
			{
				post(sid);
			}

			@Override
			public void onLoadMore()
			{
				// TODO Auto-generated method stub

			}
		});

		feature_list.setOnCancelListener(new OnCancelListener()
		{

			@Override
			public void onCancel()
			{
				// TODO Auto-generated method stub

			}
		});
		feature_list.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3)
			{
				if (arg2 > 0)
				{
					FindScenicStrategyScenic strategyScenic = (FindScenicStrategyScenic) features
							.get(arg2 - 1);
					postDetails(strategyScenic.getId());
				}

			}
		});
	}

	public void post(String id)
	{
		features.clear();
		feature_list.mState = 2;
		feature_list.changeHeaderViewByState();
		AjaxParams params = new AjaxParams();
		params.put("f", "30");
		params.put("sid", id);
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				dismissLoadingDialog();
				feature_list.onRefreshComplete();
			}

			@Override
			public void onSuccess(Object o)
			{
				String json = o.toString();
				System.out.println(json);
				try
				{
					JSONArray array = new JSONArray(json);
					FindScenicStrategyScenic strategyScenic;
					for (int i = 0; i < array.length(); i++)
					{
						JSONObject object = array.getJSONObject(i);
						String id = object.getString("id");
						String name = object.getString("name");
						String arrived = object.getString("star");
						String wantGo = object.getString("price");
						String img = object.getString("img");
						strategyScenic = new FindScenicStrategyScenic(id, name,
								arrived, wantGo, img);
						features.add(strategyScenic);
					}
					dismissLoadingDialog();
					feature_list.onRefreshComplete();
				} catch (JSONException e)
				{
					dismissLoadingDialog();
					feature_list.onRefreshComplete();
					e.printStackTrace();
				}
			}
		});
	}

	public void postDetails(String id)
	{
		showLoadingDialog("数据加载中...");
		AjaxParams params = new AjaxParams();
		params.put("f", "31");
		params.put("id", id);
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				dismissLoadingDialog();
			}

			@Override
			public void onSuccess(Object o)
			{
				String json = o.toString();
				System.out.println(json);
				try
				{
					Hotel hotel = null;
					JSONObject object = new JSONObject(json);
					String img0 = null;
					String img1 = null;
					String img2 = null;
					String img3 = null;
					String img4 = null;
					if (object.has("img0"))
					{
						img0 = object.getString("img0");
					}
					if (object.has("img1"))
					{
						img1 = object.getString("img1");
					}
					if (object.has("img2"))
					{
						img2 = object.getString("img2");
					}
					if (object.has("img3"))
					{
						img3 = object.getString("img3");
					}
					if (object.has("img4"))
					{
						img4 = object.getString("img4");
					}
					String name = object.getString("name");
					String ads = object.getString("ads");
					String tel = object.getString("tel");
					String price = object.getString("price");
					String star = object.getString("star");
					String ptss = object.getString("ptss");
					String roomType = object.getString("roomType");
					String content = object.getString("content");
					double lat = object.getDouble("lat");
					double lng = object.getDouble("lng");
					hotel = new Hotel(img0, img1, img2, img3, img4, name, ads,
							tel, price, star, ptss, roomType, content, lat, lng);
					mApplication.listHotel.add(hotel);
					dismissLoadingDialog();
					startActivity(ActivityHotelExamine.class);
				} catch (JSONException e)
				{
					dismissLoadingDialog();
					e.printStackTrace();
				}
			}
		});
	}

	public void topBack(View v)
	{
		this.finish();
	}

	public class FeatureAdapter extends BaseObjectListAdapter
	{

		public FeatureAdapter(BaseApplication application, Context context,
				List<? extends Entity> datas)
		{
			super(application, context, datas);
		}

		@Override
		public View getView(int arg0, View convertView, ViewGroup arg2)
		{
			HolderView holder;
			if (convertView == null)
			{
				holder = new HolderView();
				convertView = mInflater.inflate(R.layout.item_play, null);
				holder = new HolderView();
				holder.icon = (ImageView) convertView
						.findViewById(R.id.play_item_icon);
				holder.name = (TextView) convertView
						.findViewById(R.id.play_item_name);
				holder.price = (TextView) convertView
						.findViewById(R.id.play_item_prices);
				holder.content = (TextView) convertView
						.findViewById(R.id.play_item_content);
				holder.time = (TextView) convertView
						.findViewById(R.id.play_item_time);
				holder.middle = (LinearLayout) convertView
						.findViewById(R.id.play_item_middle);
				holder.stars = (LinearLayout) convertView
						.findViewById(R.id.play_item_star);
				holder.star1 = (ImageView) convertView.findViewById(R.id.star1);
				holder.star2 = (ImageView) convertView.findViewById(R.id.star2);
				holder.star3 = (ImageView) convertView.findViewById(R.id.star3);
				holder.star4 = (ImageView) convertView.findViewById(R.id.star4);
				holder.star5 = (ImageView) convertView.findViewById(R.id.star5);
				convertView.setTag(holder);
			} else
			{
				holder = (HolderView) convertView.getTag();
			}
			FindScenicStrategyScenic strategyScenic = (FindScenicStrategyScenic) mDatas
					.get(arg0);
			mApplication.imageLoader.displayImage(strategyScenic.getImg(),
					holder.icon, mApplication.options);
			holder.name.setText(strategyScenic.getName());
			holder.time.setVisibility(View.GONE);
			if (strategyScenic.getArrived().equals("0"))
			{
				holder.star1.setVisibility(View.GONE);
				holder.star2.setVisibility(View.GONE);
				holder.star3.setVisibility(View.GONE);
				holder.star4.setVisibility(View.GONE);
				holder.star5.setVisibility(View.GONE);
			} else if (strategyScenic.getArrived().equals("1"))
			{
				holder.star1.setVisibility(View.GONE);
				holder.star2.setVisibility(View.GONE);
				holder.star3.setVisibility(View.GONE);
				holder.star4.setVisibility(View.GONE);
			} else if (strategyScenic.getArrived().equals("2"))
			{
				holder.star1.setVisibility(View.GONE);
				holder.star2.setVisibility(View.GONE);
				holder.star3.setVisibility(View.GONE);
			} else if (strategyScenic.getArrived().equals("3"))
			{
				holder.star1.setVisibility(View.GONE);
				holder.star2.setVisibility(View.GONE);
			} else if (strategyScenic.getArrived().equals("4"))
			{
				holder.star1.setVisibility(View.GONE);
			}
			holder.price.setText("￥" + strategyScenic.getWantGo());

			return convertView;
		}

	}

	@Override
	public void onBackPressed()
	{
		finish();
	}

	public class HolderView
	{
		ImageView icon;
		LinearLayout stars;
		LinearLayout middle;
		TextView name;
		TextView time;
		TextView price;
		TextView content;
		ImageView star1;
		ImageView star2;
		ImageView star3;
		ImageView star4;
		ImageView star5;
	}

}
